<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>runlink</title>
<style type="text/css">
a{
	text-decoration:none;
	color:black;
}
</style>
</head>
<?php
	$linkhead = "http://bbs.pp960.com/dy/";
	$year = date("Y");
	$mounth = date("n");
	$day = date("d");
	$fileName = "goodlink".$year.$mounth.$day.".txt";//历史link保存文件
?>
<form action="runlink.php" method="post">
    今天：<?php echo $year."-".$mounth."-".$day;?>&nbsp;&nbsp;&nbsp;<input name="submit" type="submit" value="搜查" />&nbsp;&nbsp;&nbsp;<a href="runlink.php">刷新</a>
</form>
<?php 
	if(file_exists($fileName)){
?>
<form action="runlink.php" method="post">
	<input name="submitDownImage" type="submit" value="下载图片" />
</form>
<?php
	}
?>
<?php 
	if(!empty($_POST['submit'])){
		$maxDay = 31;
		
		if(!file_exists($fileName)){
			//当前月份之前的所有link组合
			$linkArray = array();
			for($mounthNum=1;$mounthNum<=$mounth;$mounthNum++){
				for($dayNum=1;$dayNum<=$maxDay;$dayNum++){
					$linkArray[$year."-".$mounthNum."-".$dayNum] = $linkhead.$year."-".$mounthNum."-".$dayNum.".htm";
				}
			}
			
			//根据link查找内容
			$goodLinkArray = array();
			$ch = curl_init();
			foreach($linkArray as $key=>$val){
				curl_setopt($ch,CURLOPT_URL,$val);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($ch,CURLOPT_HEADER,1);
				curl_exec($ch);//获取内容
				$info = curl_getinfo($ch);
				if($info['http_code']=='404'){
					//echo "<a href='".$val."' target='_blank' style='color:red;'>".$key."</a><br>";
				}else{
					echo "<a href='".$val."' target='_blank'>".$key."</a><br>";
					$goodLinkArray[$key] = $val;
				}
			}
			curl_close($ch);
			
			//写入文件
			$file = fopen($fileName,'ab');
			foreach($goodLinkArray as $key=>$val){
				fwrite($file, $key."---".$val."\n"); //插入第一条记录
			}
			fclose($file); //关闭文件
			echo "<span style='color:green'>当天历史记录保存成功!</span>";
		}else{
			$linkArrayList = file($fileName);
			$linkArray = array();
			foreach($linkArrayList as $val){
				$tmpLink = explode('---', $val);
				$linkArray[$tmpLink[0]] = $tmpLink[1]; 
			}
			
			foreach($linkArray as $key=>$val){
				echo "<a href='".$val."' target='_blank'>".$key."</a><br>";
			}
		}
	}else if(!empty($_POST['submitDownImage'])){
		$linkArrayList = file($fileName);//获取本地历史记录的有效链接
		$linkArray = array();//保存有效链接
		foreach($linkArrayList as $val){
			$tmpLink = explode('---', $val);
			$linkArray[$tmpLink[0]] = $tmpLink[1]; //key将用于判断是否已经存在下载图片文件夹，不重复下载
		}
		
		foreach($linkArray as $dir=>$linkUrl){
			if(is_dir($dir)){
				//echo "已存在图片".$dir;echo "<br>";
			}else{
				$imgfile = file_get_contents(trim($linkUrl));//打开链接文件获取内容
				$preg = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';//正规找到img内容
				preg_match_all($preg,$imgfile,$imgSrcList);//获取内容中img的数据集合
				$imgSrcTxt = $imgSrcList[0][1];//获取img数据
				$imgSrcArray = explode("<br />",$imgSrcTxt);//将逐个img分割开
				$downImgSrcArray = array();//保存图片下载链接数组
				$downImgTypeArray = array();//保存图片类型数组
				foreach($imgSrcArray as $imgSrc){
					$imgSrcList = array();
					preg_match($preg,$imgSrc,$imgSrcList);
					$downImgSrcArray[] = $imgSrcList[1];//获取图片src
					$downImgTypeArray[] = $imgSrcList[2];//获取图片类型
				}
				//下载图片
				foreach($downImgSrcArray as $downImgNum=>$downImgSrc){
					downImgFile($downImgSrc,$dir,$downImgNum.'.'.$downImgTypeArray[$downImgNum]);
				}
			}
		}
	}
	//保存图片
	function downImgFile($url,$folder,$pic_name){	
		set_time_limit(24*60*60); //限制最大的执行时间
		if(!is_dir($folder)){
			mkdir($folder);
		}
		$destination_folder=$folder.'/'; //文件下载保存目录
		$newfname=$destination_folder.$pic_name;//文件PATH
		$file=fopen($url,'rb');
		
		if($file){			
			$newf=fopen($newfname,'wb');
			if($newf){				
				while(!feof($file)){					
					fwrite($newf,fread($file,1024*8),1024*8);
				}
			}
			if($file){				
				fclose($file);
			}
			if($newf){
				echo "<span style='color:green'>下载".$newfname."成功！</span><br>";				
				fclose($newf);
			}
		}		
	}	
?>
<body>
</body>
</html>